<?php

require_once("database.php");
require_once '../DTO/TAISAN_DTO.php';

class xl_loai_tai_san extends database
{
	function danh_sach_theo_loai( $vi_tri, $so_luong,$ma_loai_tai_san)
	{
		$sql = "SELECT ts . *
                FROM tai_san ts, loai_tai_san lts
                WHERE ts.loai_tai_san = lts.ma
                        AND lts.ma= '$ma_loai_tai_san'
                LIMIT 0 , 30";
		$this->setQuery($sql);
		return $this->loadAllRow();
	}
	function danh_sach_tim_kiem($key, $vi_tri, $so_luong)
	{
		$sql = "select * from tai_san where ten like '%$key%' limit $vi_tri, $so_luong";
		$this->setQuery($sql);
		return $this->loadAllRow();
	}
	function danh_sach_ban_chay()
	{
		$sql = "SELECT sp.*, ct.ma_tai_san, sum(ct.so_luong) tong_so_luong
				FROM tai_san sp, chi_tiet_hoa_don ct
				WHERE sp.ma = ct.ma_tai_san
				GROUP BY ct.ma_tai_san
				ORDER BY tong_so_luong DESC
				LIMIT 0, 10";
		$this->setQuery($sql);
		return $this->loadAllRow();
	}
	function danh_sach_cung_loai($ma, $ma_loai_tai_san)
	{
		$sql = "select * from tai_san where ma_loai_tai_san = $ma_loai_tai_san and ma != $ma ORDER BY RAND() limit 0, 6";
		$this->setQuery($sql);
		return $this->loadAllRow();
	}
	function danh_sach_theo_tai_san_moi()
	{
		$sql = "select * from tai_san where tai_san_moi = 1 ";
		$this->setQuery($sql);
		return $this->loadAllRow();
	}
	function danh_sach($vi_tri, $so_luong)
	{
		$vi_tri = intval($vi_tri);
		$so_luong = intval($so_luong);
		$sql = "select * from tai_san limit $vi_tri, $so_luong";
		$this->setQuery($sql);
		return $this->loadAllRow();
	}
	function danh_sach_all()
	{
		$sql = "select * from loai_tai_san ";
		$this->setQuery($sql);
		return $this->loadAllRow();
	}
	function so_luong($ma_loai_tai_san)
	{
		$sql = "select count(*) from tai_san where ma_loai_tai_san = '$ma_loai_tai_san' ";
		$this->setQuery($sql);
		return $this->loadResult();
	}
	function xem($ma)
	{
		$ma = intval($ma);

		$sql = "select * from tai_san where ma = '$ma'";
		$this->setQuery($sql);
		return $this->loadRow();
	}
	function xoa($ma)
	{
		$ma = intval($ma);

		$sql = "delete from tai_san where ma = '$ma'";
		$this->setQuery($sql);
		return $this->query();
	}
	function them(TAISAN_DTO $info)
	{
		$info->ten_tai_san = addslashes($info->ten_tai_san);
        $info->ten_tai_san = intval($info->ten_tai_san);

        $info->dia_chi = addslashes($info->dia_chi);
        $info->dia_chi = intval($info->dia_chi);

        $info->gia = addslashes($info->gia);
        $info->gia = intval($info->gia);

        $info->thong_tin_chi_tiet = addslashes($info->thong_tin_chi_tiet);
        $info->thong_tin_chi_tiet = intval($info->thong_tin_chi_tiet);



		$sql = "INSERT INTO tai_san VALUES
                            (
                                    '$info->ten_tai_san',
                                    '$info->dia_chi',
                                    '$info->gia',
                                    '$info->loai_tai_san',
                                    '$info->thong_tin_chi_tiet',
                                    0
                            )";
		$this->setQuery($sql);
		return $this->query();
	}
	
	function cap_nhat($ma, $ten, $ma_loai_tai_san, $mo_ta_tom_tat, $mo_ta_chi_tiet, $don_gia, $hinh, $tai_san_moi, $so_lan_xem, $ngay_tao, $trang_thai)
	{
		$ma = intval($ma);
		$ten = addslashes($ten);
		$ma_loai_tai_san = intval($ma_loai_tai_san);
		$mo_ta_tom_tat = addslashes($mo_ta_tom_tat);
		$mo_ta_chi_tiet = addslashes($mo_ta_chi_tiet);
		$don_gia = intval($don_gia);
		$hinh = addslashes($hinh);
		$so_lan_xem = intval($so_lan_xem);

		$sql ="UPDATE tai_san SET ten= '$ten', ma_loai_tai_san= '$ma_loai_tai_san', mo_ta_tom_tat= '$mo_ta_tom_tat', mo_ta_chi_tiet= '$mo_ta_chi_tiet', don_gia= '$don_gia', hinh= '$hinh', tai_san_moi= '$tai_san_moi', so_lan_xem= '$so_lan_xem', ngay_tao= '$ngay_tao', trang_thai= '$trang_thai' WHERE ma = '$ma'";
		$this->setQuery($sql);
		return $this->query();
	}
	
	function cap_nhat_trang_thai($ma)
	{
		$ma = intval($ma);

		$sql = "UPDATE tai_san SET trang_thai = 1 - trang_thai WHERE ma = '$ma'";
		$this->setQuery($sql);
		return $this->query();
	}
	
	function cap_nhat_tai_san_moi($ma)
	{
		$ma = intval($ma);

		$sql = "UPDATE tai_san SET tai_san_moi = 1 - tai_san_moi WHERE ma = '$ma'";
		$this->setQuery($sql);
		return $this->query();
	}
	
	function tim_kiem ($tu_khoa)
	{
		//$sql = "select * from tai_san where	$ten like %tu_khoa% or 	$mo_ta_tom_tat like %tu_khoa% or 	$mo_ta_chi_tiet like %tu_khoa% or 	$hinh like %tu_khoa%";
		//$this->setQuery($sql);
		return $this->loadAllRow();	
	}
 }
?>